package com.jiashihui.schema_lgorithm.java.properties.util;

import com.jiashihui.schema_lgorithm.common.ExceptionEnum;
import com.jiashihui.schema_lgorithm.exception.AncbException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * safeProperties util
 */
public class PropertiesUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(PropertiesUtil.class);

    private static SafeProperties safeProperties;

    /**
     * 构造器
     */
    private PropertiesUtil(){

    }

    /**
     * 加载文件
     *
     * @throws AncbException
     */
    public static void load(String filePath) throws AncbException {
        safeProperties = new SafeProperties();
        try (FileInputStream fis = new FileInputStream(filePath)) {
            safeProperties.load(fis);
        } catch (IOException e) {
            LOGGER.error("文件读取异常.", e);
            throw new AncbException(ExceptionEnum.NOT_FOUND_FILE_EXCEPTION.getCode(), e);
        }
    }

    /**
     * 操作文件
     *
     * @param key
     * @param value
     * @throws AncbException
     * @return
     */
    public static Boolean optionProperty(String filePath, String key, String value) throws AncbException {
        try (FileOutputStream fos = new FileOutputStream(filePath)) {
            if (safeProperties == null) {
                load(filePath);
            }
            safeProperties.put(key, value);
            // 将safeProperties集合保存到流中
            safeProperties.store(fos,null);
        } catch (IOException e) {
            LOGGER.error("文件读取异常.", e);
            throw new AncbException(ExceptionEnum.NOT_FOUND_FILE_EXCEPTION.getCode(), e);
        }
        return true;
    }

    /**
     * 修改
     *
     * @param key
     * @param value
     * @throws AncbException
     * @return
     */
    public static Boolean updateProperty(String filePath, String key, String value) throws AncbException {
        if (getProperty(filePath, key) == null) {
            LOGGER.info("没有对应的{}属性.", key);
            throw new AncbException(ExceptionEnum.NO_PROPERTY_EXCEPTION.getCode(), ExceptionEnum.NO_PROPERTY_EXCEPTION.getMsg());
        }
        return optionProperty(filePath, key, value);
    }

    /**
     * 新增
     *
     * @param key
     * @param value
     * @throws AncbException
     * @return
     */
    public static Boolean addProperty(String filePath, String key, String value) throws AncbException {
        if (getProperty(filePath, key) != null) {
            LOGGER.info("{}对应的属性已存在.", key);
            throw new AncbException(ExceptionEnum.PROPERTY_EXIST_EXCEPTION.getCode(), ExceptionEnum.PROPERTY_EXIST_EXCEPTION.getMsg());
        }
        return optionProperty(filePath, key, value);
    }

    /**
     * 获取属性值
     *
     * @param key
     * @throws AncbException
     * @return
     */
    public static String getProperty(String filePath, String key) throws AncbException {
        if (safeProperties == null) {
            load(filePath);
        }
        return safeProperties.getProperty(key);
    }

    /**
     * 清楚
     */
    public static void clear(){
        safeProperties = null;
    }
}
